package com.animerank.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.animerank.service.IFavoriteService;
import com.animerank.dto.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 收藏控制器
 */
@RestController
@RequestMapping("/favorite")
@Tag(name = "收藏管理", description = "动漫收藏相关接口")
public class FavoriteController {

    @Autowired
    private IFavoriteService favoriteService;

    /**
     * 添加收藏接口
     */
    @PostMapping("/{animeId}")
    @SaCheckLogin
    @Operation(summary = "添加收藏", description = "用户收藏动漫，需要登录")
    public Result<?> addFavorite(@PathVariable Long animeId) {
        return favoriteService.addFavorite(animeId);
    }

    /**
     * 取消收藏接口
     */
    @DeleteMapping("/{animeId}")
    @SaCheckLogin
    @Operation(summary = "取消收藏", description = "用户取消收藏动漫，需要登录")
    public Result<?> removeFavorite(@PathVariable Long animeId) {
        return favoriteService.removeFavorite(animeId);
    }

    /**
     * 检查收藏状态接口
     */
    @GetMapping("/check/{animeId}")
    @Operation(summary = "检查收藏状态", description = "检查当前用户是否收藏了指定动漫")
    public Result<Boolean> checkFavorite(@PathVariable Long animeId) {
        return favoriteService.checkFavorite(animeId);
    }

    /**
     * 获取用户收藏ID列表接口（支持分页）
     */
    @GetMapping("/user/list")
    @SaCheckLogin
    @Operation(summary = "获取用户收藏ID列表", description = "获取当前登录用户收藏的动漫ID列表")
    public Result<Map<String, Object>> getUserFavoriteIds(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "9") Integer pageSize) {
        return favoriteService.getUserFavoriteIds(page, pageSize);
    }

    /**
     * 获取动漫收藏数量接口
     */
    @GetMapping("/count/{animeId}")
    @Operation(summary = "获取动漫收藏数量", description = "获取指定动漫的收藏数量")
    public Result<Integer> getAnimeFavoriteCount(@PathVariable Long animeId) {
        return favoriteService.getAnimeFavoriteCount(animeId);
    }
    
    /**
     * 批量检查收藏状态接口
     */
    @PostMapping("/batch-check")
    @Operation(summary = "批量检查收藏状态", description = "批量检查当前用户是否收藏了多个动漫")
    public Result<Map<Long, Boolean>> batchCheckFavorites(@RequestBody List<Long> animeIds) {
        return favoriteService.batchCheckFavorites(animeIds);
    }
}